home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PsL Monthly 1993 December
/
PSL Monthly Shareware CD-ROM (December 1993).iso
/
prgmming
/
dos
/
c
/
comstr.exe
/
READ.ME
< prev
next >
Wrap
Text File
|
1992-12-01
|
13KB
|
390 lines
Patrick Reilly
CIS: 70274,161
This file represents an upgrade for the older COMST1.ZIP. That file had
problems. Besides being cryptic and only a streambuf-derived class, there
was a bug in the combuf::extractc() method. This has been fixed in this
version.
As an addition, I've included a 'wrapper' iostream-derived class for
people not overly familiar with the C++ stream classes. I've included
manipulators for setting protocol (baud, parity, etc). Also new is this
readme file!
Description:
struct ComDef
-------------------------------------------------------------------------
This structure is used internally by combuf to keep track of who owns
what port, etc. In general, you can ignore it.
struct CommProtocol
-------------------------------------------------------------------------
This structure encapsulates all the protocol settings for a port: baud
rate, number of bits, parity, and stopbits. It is accepted as an argument
to combuf::setParams(), and exists mainly for people wanting to directly
play with combuf.
class combuf
-------------------------------------------------------------------------
This is the heart and sole of serial streaming. The combuf class is
derived from streambuf, and handles the intricacies of serial
communication.
Methods
-------
combuf( char *s, int i, int o ) constructor
This sets up an unopen buffer (not attached to any serial port), with
the input buffer at s[0]..s[i-1], and the output buffer at
s[i]..s[i+o-1]. Unlike most streambufs, combuf actually uses two
seperate areas of its internal buffer - an input part, and an output
part. This is because you really don't want to mix the two: you want
input to be inserted by the serial port (and extracted by your code)
and output (inserted by your code) to be extracted by the serial port.
combuf() constructor
This sets up an unopen buffer with no internal buffer. Note that you
will not be able to use (open) this until a call is made to setbuf():
combuf requires an internal buffer (does not operate in unbuffered
mode).
~combuf() destructor
Performs housekeeping (flush/close) and destroys the combuf.
streambuf *setbuf( char *buf, int sz )
Since combuf actually uses two areas of a buffer, this method just
calls setbuf( buf, sz/2, sz/2 ).
combuf *setbuf( char *buf, int isz, int osz )
This sets the internal buffer input area to buf[0]..buf[isz-1] and
the output area to buf[isz]..buf[isz+osz-1].
int underflow()
This method is called whenever you want to extract from the combuf
but the serial port hasn't put any data in it. It will wait the
timeout period for a char to appear: if so, everythings fine. If not,
returns EOF which will signal the owning stream to set an error flag.
int do_sputn( char *s, int n )
This method puts your output into the buffer and handles serial-
related issues (priming the pump) to make sure it gets sent.
int overflow(int)
Called whenever the internal buffer is full and you still want to
send characters. Flushes the buffer and puts the char in there. It
checks for timeout errors, and will return EOF if one occurs, which
will signal the owning stream to set an error flag.
void setPort( int p )
'Opens' a serial port for use. The port number (p) MUST be in the
range 0 (COM1) to 3 (COM4).
void setTimeout( long t )
Sets the timeout period to t, where t is in 1/100ths of a second.
Note that, though the argument is in 1/100ths of a second, the actual
resolution of the timeout timer is in +/- 55mSec. It always rounds
up, so setTimeout(1) will set timeout_ to 1, NOT 0.
void setInterCharDelay( long t )
Sets the intercharacter delay to t, where t is in 1/100ths of a
second. The intercharacter delay is the amount of time paused between
sending consecutive characters; for some systems (and many modems),
some delay is required between the chars.
void setInterLineDelay( long t )
Sets the interline delay to t, where t is in 1/100ths of a second.
The interline delay is the amount of time paused after sending a
carriage return (ascii 13). Some systems are line-oriented (ie they
buffer a line, then process it) and so require a delay after a line
is sent.
void setOption( int bit )
Sets options flags. Valid bits are: HW_HANDSHAKE (use hardware
handshaking: not currently implemented), SW_HANDSHAKE (use software
handshaking (XON/XOFF): not currently implemented), and WAITFORECHO
(pause after sending a char until its echo is received: not currently
implemented).
void clearOption( int bit )
Clears option flag (see setOption() for flags).
void setBaud( long b )
Sets the baud rate to b. Must be in the range 50 <= b <= 115200. Note
that the baud rate is changed ONLY if the port is open.
void setParity( int p )
Sets the parity to p (one of: combuf::none, combuf::odd, combuf::even).
Note that the port must be open for the parity to change.
void setBits( int b )
Sets the number of bits in the protocol: must be 6 <= b <= 8. Note the
port MUST be open for the bits to change.
void setStopBits( int b )
Sets the number of stop bits in the protocol: must be 1 <= b <= 2.
Note that the port MUST be open for the stop bits to change.
void setParams( CommProtocol& p )
Sets the baud, bits, parity, and stopbits as contained in p. Note that
the port MUST be open for the changes to occur.
void assertDTR()
If the port is open, asserts the DTR (Data Terminal Ready) signal.
THIS IS NOT DONE AUTOMATICALLY BY SETPORT()! Note that a modem will
ignore you unless you assert DTR.
void unassertDTR()
If the port is open, unasserts the DTR signal. This will disable
communications with a modem (if it was off-hook, it will hang up).
int status()
If timeout, line, or modem errors have occured, they set an internal
status flag. Calling this method will return the status, then clear
it.
int extractc()
Used by the ISR (interrupt service routine) to get the next char to
be sent out the serial port.
int insertc( int ch )
Used by the ISR to insert ch (incoming byte) to the buffer.
unsigned long timeToTicks( unsigned long t )
Converts t (1/100ths of a second) to system ticks (18.2 per second).
void assertToPort( int offs, int mask )
Used internally by combuf to assert (set) bits to a port.
void unassertToPort( int offs, int mask )
Used internally by combuf to unassert (clear) bits to a port.
void interrupt far handler0x0B(...)
This is the ISR that services interrupts for COM2/4.
void interrupt far handler0x0C(...)
This is the ISR that services interrupts for COM1/3.
void handleIRQ( int port, int cause, int data )
This is the method that is called when an interrupt on the combuf's
port occurs.
Members
-------
ComDef def[4]
Holds information for each port (COM1,2,3,4).
enum ParityType { none, odd, even }
Used for the argument to setParity().
int offs_
The offset into def for this combuf. If EOF, means this combuf is
closed. Otherwise (0..3) this combuf is open.
int errFlags
Holds error flags; returned/cleared by status().
int options
Holds the option flags. Bits are set with setOption() and cleared
with clearOption().
int inSize
Size of the input buffer.
unsigned long timeout_
Timeout time (in ticks).
unsigned long ibdelay_
Intercharacter delay time (in ticks).
unsigned long ildelay_
Interline delay time (in ticks).
class comstream
-------------------------------------------------------------------------
This class is derived from iostream and presents an iostream 'wrapper'
for combuf.
Methods
-------
comstream() constructor
Initializes a comstream with no buffer. Note that you can't use
this stream until a call to rdbuf()->setbuf() is made.
comstream( char *b, int i, int o ) constructor
Initializes a comstream